<?php

// 获取活动分页列表(后台)
// GET activity.admin.volist

namespace app\api\resource\activity\admin;
use think\Db;
use app\api\lib\Page;

class Volist
{
    // 方法
    public $_method = ['GET'];
    // 前置
    public $_pre    = ['adminLogin'];
    // 描述
    public $_description = '获取活动分页列表(后台)';
    // 参数
    public $_param  = [
        'page'  => '请求页,默认1',
        'show'  => '请求记录数量,默认10',
        'type'  => '类型,0-全部(默认) 1-自办型 2-外链型',
        'model' => '模式,0-全部(默认) 1-未开始 2-进行中 3-已结束',
        'skey'  => '搜索关键词,活动名称',
    ];
    // 返回
    public $_return = [
        200 => '获取成功',
    ];

    public function run(&$request)
    {
        $page  = $request->get('page/d',1);
        $show  = $request->get('show/d',10);

        $type  = $request->get('type/d',0);
        $model = $request->get('model/d',0);
        $skey  = $request->get('skey/s','','trim');

        $table = 'activity';
        $where = '';

        // 类型
        if (1 == $type) {
            $where['type'] = 0;
        } elseif (2 == $type) {
            $where['type'] = 1;
        }
        // 模式
        switch ($model) {
            case 0: // 全部(默认)
                break;
            case 1: // 未开始
                $where['start_time'] = ['>',time()];
                break;
            case 2: // 进行中
                $where['start_time'] = ['<',time()];
                $where['end_time']   = ['>',time()];
                break;
            case 2: // 已结束
                $where['end_time']   = ['<',time()];
                break;
            default:
                break;
        }
        // 搜索活动名称
        if ( !empty($skey) ) {
            $where['title'] = ['like','%' . $skey . '%'];
        }

        $list = Db::name($table)->where($where)->order('id desc')->page($page,$show)->select();

        // 填装报名统计
        if ( !empty($list) ) {
            foreach ($list as $key => $val) {
                if (0 == $val['type']) {
                    $list[$key]['signup'] = Db::name('activity_signup')->where('activity_id',$val['id'])->count();
                }
            }
        }

        $pagelist = Page::run($table,$page,$show,$where,$list);

        return [200,$pagelist];
    }
}